import quotationApi from "@/api/modules/quotation";
import Vue from "vue";
import {
  checkAddressErrors,
  checkWholeErrors,
  checkFromAddressErrors,
  checkToAddressErrors,
  checkLoadErrors
} from '@/views/newQuotation/corePage/index'
import { loadAreaTree } from "@packages/basic/area/area.js"

const QUOTATION_DATA = {
  WHOLE: 'WHOLE',
  PICK: 'PICK',
  DELIVER: 'DELIVER',
  LOAD: 'LOAD',
  UNLOAD: 'UNLOAD',
  DIVIDE: 'DIVIDE',
  // DIVIDE_SET_DATA: 'DIVIDE_SET_DATA',
  DETAIL_DATA: 'DETAIL_DATA',
  OTHER: 'OTHER',
  BARGA_LATER_LIST: 'BARGA_LATER_LIST',
  RESPONSE: 'RESPONSE',
  BARGA_WHOLE: 'BARGA_WHOLE',
  BARGA_DIVIDE: 'BARGA_DIVIDE',
  BARGA_DELIVER: 'BARGA_DELIVER',
  BARGA_LOAD: 'BARGA_LOAD',
  BARGA_PICK: 'BARGA_PICK',
  BARGA_UNLOAD: 'BARGA_UNLOAD',
};

const state = {
  /* 整车 */
  whole: [],
  /* 提货 */
  pick: [],
  /* 送货 */
  deliver: [],
  /* 装货 */
  load: [],
  /* 卸货 */
  unload: [],
  /* 零担 */
  divide: [],
  detailData: {},
  /* 其它费用 */
  other: {},
  /* 议价数据 */
  bargaWhole: [],
  bargaDivide: [],
  bargaDeliver: [],
  bargaLoad: [],
  bargaPick: [],
  bargaUnload: [],
  response: false
};

const mutations = {
  [QUOTATION_DATA.WHOLE] (state, whole = []) {
    state.whole = whole;
  },
  [QUOTATION_DATA.PICK] (state, pick = []) {
    state.pick = pick;
  },
  [QUOTATION_DATA.DELIVER] (state, deliver = []) {
    state.deliver = deliver;
  },
  [QUOTATION_DATA.LOAD] (state, load = []) {
    state.load = load;
  },
  [QUOTATION_DATA.UNLOAD] (state, unload = []) {
    state.unload = unload;
  },
  /* 零担 */
  [QUOTATION_DATA.DIVIDE] (state, divide = []) {
    state.divide = divide;
  },

  [QUOTATION_DATA.BARGA_WHOLE] (state, bargaWhole = []) {
    state.bargaWhole = bargaWhole;
  },
  [QUOTATION_DATA.BARGA_DIVIDE] (state, bargaDivide = []) {
    state.bargaDivide = bargaDivide;
  },
  [QUOTATION_DATA.BARGA_DELIVER] (state, bargaDeliver = []) {
    state.bargaDeliver = bargaDeliver;
  },
  [QUOTATION_DATA.BARGA_LOAD] (state, bargaLoad = []) {
    state.bargaLoad = bargaLoad;
  },
  [QUOTATION_DATA.BARGA_PICK] (state, bargaPick = []) {
    state.bargaPick = bargaPick;
  },
  [QUOTATION_DATA.BARGA_UNLOAD] (state, bargaUnload = []) {
    state.bargaUnload = bargaUnload;
  },
  // [QUOTATION_DATA.DIVIDE_SET_DATA] (state, divideSetData = {}) {
  //   const divide = divideSetData.map(item => {
  //     if (item.tableData.length) {
  //       return item.tableData
  //     }
  //   });
  //   state.divide = divide;
  //   console.log('divide组合', divide)
  //   // state.divideSetData = divideSetData;
  // },
  [QUOTATION_DATA.DETAIL_DATA] (state, detailData = {}) {
    state.detailData = detailData;
  },
  [QUOTATION_DATA.OTHER] (state, other = {}) {
    state.other = other;
  },
  [QUOTATION_DATA.RESPONSE] (state, response) {
    state.response = response;
  },

  vehicleClearer (state, index) {
    state.whole.splice(index, 1)
  }
};

const actions = {
  async buffWhole ({ commit }, { data }) {
    const duplicate = data
    commit(QUOTATION_DATA.WHOLE, duplicate);
  },

  async buffPick ({ commit }, { data }) {
    const duplicate = data
    commit(QUOTATION_DATA.PICK, duplicate);
  },

  async buffDeliver ({ commit }, { data }) {
    const duplicate = data
    commit(QUOTATION_DATA.DELIVER, duplicate);
  },

  async buffLoad ({ commit }, { data }) {
    const duplicate = data
    commit(QUOTATION_DATA.LOAD, duplicate);
  },

  async buffunload ({ commit }, { data }) {
    const duplicate = data
    commit(QUOTATION_DATA.UNLOAD, duplicate);
  },

  async buffDivide ({ commit }, { data }) {
    const duplicate = data
    console.log('duplicate零担:::::', duplicate)
    commit(QUOTATION_DATA.DIVIDE, duplicate);
  },
  // 零担议价数据
  async buffbargaDivide ({ commit }, { data }) {
    commit(QUOTATION_DATA.BARGA_DIVIDE, data);
  },
  // 整车议价数据
  async buffbargaWhole ({ commit }, { data }) {
    commit(QUOTATION_DATA.BARGA_WHOLE, data);
  },
  // 送货议价数据
  async buffbargaDeliver ({ commit }, { data }) {
    commit(QUOTATION_DATA.BARGA_DELIVER, data);
  },
  // 提货议价数据
  async buffbargaPick ({ commit }, { data }) {
    commit(QUOTATION_DATA.BARGA_PICK, data);
  },
  // 装货议价数据
  async buffbargaLoad ({ commit }, { data }) {
    commit(QUOTATION_DATA.BARGA_LOAD, data);
  },
  // 卸货议价数据
  async buffbargaUnload ({ commit }, { data }) {
    commit(QUOTATION_DATA.BARGA_UNLOAD, data);
  },

  async clearerVehicle ({ commit }, { index }) {
    commit('vehicleClearer', index);
  },

  // 发货方新增
  async customerAdd ({ commit }, { params }) {
    return new Promise((resolve) => {
      quotationApi.addCustomer(params).then(response => {
        if (response.code === 0) {
          resolve(response)
        } else {
          Vue.prototype.$message({ type: "error", message: response.message });
        }
      })
    })
  },
  // 发货方更新
  async customerUpdate ({ commit }, { templateId, params }) {
    return new Promise((resolve) => {
      quotationApi.updateCustomer(templateId, params).then(response => {
        if (response.code === 0) {
          resolve(response)
        } else {
          Vue.prototype.$message({ type: "error", message: response.message });
        }
        // resolve(response)
        // Vue.prototype.$message({ type: "success", message: "操作成功" });
      })
    })
  },

  // 承运方新增
  async carrierAdd ({ commit }, { params }) {
    return new Promise((resolve) => {
      quotationApi.carrierAdd(params).then(response => {
        if (response.code === 0) {
          resolve(response)
        } else {
          Vue.prototype.$message({ type: "error", message: response.message });
        }
      })
    })
  },
  // 承运方更新
  async carrierUpdate ({ commit }, { templateId, params }) {
    return new Promise((resolve) => {
      quotationApi.updateCarrier(templateId, params).then(response => {
        if (response.code === 0) {
          resolve(response)
        } else {
          Vue.prototype.$message({ type: "error", message: response.message });
        }
      })
    })
  },

  // 发货方发送
  async customerSend ({ commit }, { params }) {
    let response = quotationApi.customerSend(params)
    return response
  },
  // 承运方发送
  async carrierSend ({ commit }, { params }) {
    let response = quotationApi.carrierSend(params)
    return response
  },

  async detail ({ commit, dispatch }, { templateId, variable }) {
    commit(QUOTATION_DATA.RESPONSE, variable)
    if (!templateId) {
      return dispatch('clear')
    }
    let response = await quotationApi.getTemplate(templateId)
    const { rules, ...surplus } = response.data
    commit(QUOTATION_DATA.WHOLE, rules.whole);
    commit(QUOTATION_DATA.PICK, rules.pick);
    commit(QUOTATION_DATA.DELIVER, rules.deliver);
    commit(QUOTATION_DATA.LOAD, rules.load);
    commit(QUOTATION_DATA.UNLOAD, rules.unload);
    commit(QUOTATION_DATA.DIVIDE, rules.divide);
    commit(QUOTATION_DATA.OTHER, { insuranceRate: surplus.insuranceRate, minimumFee: surplus.minimumFee, taxRate: surplus.taxRate })
    commit(QUOTATION_DATA.DETAIL_DATA, surplus)
    commit(QUOTATION_DATA.RESPONSE, true)
  },

  async addCustomer ({ state, dispatch }, { kwargs }) {

    const params = { ...kwargs, rules: { ...state } }

    await dispatch('customerAdd', { params: params })
    dispatch('clear')
  },

  // 发货方更新
  async updateCustomer ({ state, dispatch }, { kwargs }) {

    const params = { ...kwargs, rules: { ...state } }

    await dispatch('customerUpdate', { templateId: params.templateId, params: params })
    dispatch('clear')
  },

  // 承运方更新
  async updateCarrier ({ state, dispatch }, { kwargs }) {

    const { other, ...args } = state
    const params = { ...kwargs, rules: { ...args }, ...other }

    await dispatch('carrierUpdate', { templateId: params.templateId, params: params })
    dispatch('clear')
  },

  // 承运方新增
  async addCarrier ({ state, dispatch }, { kwargs }) {
    const { other, ...args } = state

    const params = { ...kwargs, rules: { ...args }, ...other }
    await dispatch('carrierAdd', { params: params })

    dispatch('clear')
  },

  async sendCustomer ({ state, dispatch }, { kwargs }) {
    const { detailData, other, ...args } = state

    const params = { ...kwargs, rules: { ...args }, ...other }

    if (params.type === 1 || params.customerId) {
      let response = await dispatch('customerSend', { params: { templateId: params?.templateId || null, ...params } })

      if (response.code === 0) {
        await dispatch('clear')
        return Promise.resolve(true)
      } else {
        return Promise.reject(false)
      }
    } else {
      let response = await dispatch('carrierSend', { params: { templateId: params?.templateId || null, ...params } })

      if (response.code === 0) {
        await dispatch('clear')

        return Promise.resolve(true)
      } else {
        return Promise.reject(false)
      }
    }
  },
  async namedExport ({ dispatch, commit }, { rules, variable }) {
    await dispatch('clear', false)
    commit(QUOTATION_DATA.RESPONSE, variable)
    const areaOptions = await loadAreaTree();

    const { whole, pick, deliver, load, unload, divide, other } = rules
    const wholes = checkWholeErrors(whole, areaOptions)
    const picks = checkFromAddressErrors(pick, areaOptions)
    const delivers = checkToAddressErrors(deliver, areaOptions)
    const divides = checkAddressErrors(divide, areaOptions)
    const loads = checkLoadErrors(load)
    const unloads = checkLoadErrors(unload)

    setTimeout(() => {
      commit(QUOTATION_DATA.WHOLE, wholes);
      commit(QUOTATION_DATA.PICK, picks);
      commit(QUOTATION_DATA.DELIVER, delivers);
      commit(QUOTATION_DATA.LOAD, loads);
      commit(QUOTATION_DATA.UNLOAD, unloads);
      commit(QUOTATION_DATA.DIVIDE, divides);
      commit(QUOTATION_DATA.OTHER, other);
      commit(QUOTATION_DATA.RESPONSE, true)
    }, 100)
  },

  clear ({ commit }, variable) {
    commit(QUOTATION_DATA.WHOLE, []);
    commit(QUOTATION_DATA.PICK, []);
    commit(QUOTATION_DATA.DELIVER, []);
    commit(QUOTATION_DATA.LOAD, []);
    commit(QUOTATION_DATA.UNLOAD, []);
    commit(QUOTATION_DATA.DIVIDE, []);
    commit(QUOTATION_DATA.OTHER, {});
    if (!variable) return
    commit(QUOTATION_DATA.DETAIL_DATA, {});
  },
  clearBarga ({ commit }) {
    commit(QUOTATION_DATA.BARGA_WHOLE, []);
    commit(QUOTATION_DATA.BARGA_DIVIDE, []);
    commit(QUOTATION_DATA.BARGA_DELIVER, []);
    commit(QUOTATION_DATA.BARGA_LOAD, []);
    commit(QUOTATION_DATA.BARGA_PICK, []);
    commit(QUOTATION_DATA.BARGA_UNLOAD, []);
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};